package com.ruibei.ui;

import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.ruibei.R;
import com.ruibei.models.ApprovalDetailModel;
import com.ruibei.utils.BitmapUtil;
import com.ruibei.utils.FileUtil;
import com.ruibei.utils.Utils;
import com.ruibei.view.TitleBarView;

/**
 * 创建审批界面
 * @author wu
 *
 */
public class CreateApprovalActivity extends BaseActivity implements OnClickListener{
	
	protected static final int CAMERA_RESULT = 1;
	private static final int ADD_APPROVAL_DETAIL = 100;
	@InjectView(R.id.table1)
	TableLayout table1;
	@InjectView(R.id.table2)
	TableLayout table2;
	@InjectView(R.id.date_for_approval)
	EditText date_for_approval;
	@InjectView(R.id.new_approval_title_bar)
	TitleBarView new_approval_title_bar;
	@InjectView(R.id.add_approval_detail)
	TextView add_approval_detail;
	@InjectView(R.id.remove_approval_detail)
	TextView remove_approval_detail;
	@InjectView(R.id.add_picture_for_approval)
	ImageView add_picture_for_approval;
	
	private String image_path=Environment.getExternalStorageDirectory()+"/ruibei/";
	private int count=0;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_approval);
		ButterKnife.inject(this);
		initView();
	}

	private void initView() {
		new_approval_title_bar.setTitleText(R.string.create_new_approval_title);
		new_approval_title_bar.setBtnLeft(R.drawable.back, R.string.back);
		new_approval_title_bar.setBtnLeftOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		table1.setStretchAllColumns(true);
		date_for_approval.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if(arg1.getAction() == MotionEvent.ACTION_DOWN){
					Utils.getDateTime(date_for_approval, CreateApprovalActivity.this);
				}
				return true;
			}
		});
		
		add_approval_detail.setOnClickListener(this);
		add_picture_for_approval.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.remove_approval_detail:
			
			break;
		case R.id.add_approval_detail:
			Intent intent=new Intent(CreateApprovalActivity.this, AddApprovalDetailActivity.class);
			startActivityForResult(intent, 100);
			break;
		case R.id.add_picture_for_approval:
			showDialog();
			break;
		default:
			break;
		}
	}
	private void showDialog() {
		new AlertDialog.Builder(this)
				.setTitle("选择图片")
				.setPositiveButton("相机", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		                startActivityForResult(intent, CAMERA_RESULT);
					}
				})
				.setNegativeButton("图库", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						
					}
				}).show();
				
				
				
				
//				.setPositiveButton("Camera", new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						// 调用系统的拍照功能
//						Intent intent = new Intent(
//								MediaStore.ACTION_IMAGE_CAPTURE);
//						// 指定调用相机拍照后照片的储存路径
//						intent.putExtra(MediaStore.EXTRA_OUTPUT,
//								Uri.fromFile(tempFile));
//						startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
//					}
//				}).setNegativeButton("Photo", new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						Intent intent = new Intent(Intent.ACTION_PICK, null);
//						intent.setDataAndType(
//								MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
//								"image/*");
//						startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
//					}
//				}).show();
//		
		
	}
	@Override  
    protected void onActivityResult(int requestCode, int resultCode, final Intent data)  
    {  
		//可以根据多个请求代码来作相应的操作  
        if(ADD_APPROVAL_DETAIL==resultCode)  
        {  
        	ApprovalDetailModel aDetailModel =  (ApprovalDetailModel)data.getExtras().get("approval_data");
    				TableRow row = new TableRow(this);
    				row.setOnClickListener(rowClickEvent);
    				row.setOnLongClickListener(rowLongClickEvent);
    				count++;
    				row.setId(count);
    				TextView goods = new TextView(this);
    				goods.setBackgroundResource(R.drawable.table_frame_gray);
    				goods.setText(aDetailModel.getGoods());
    				goods.setPadding(0, 10, 0, 10);
    				goods.setTextSize(16f);
    				goods.setGravity(Gravity.CENTER);
    				
    				TextView count = new TextView(this);
    				count.setBackgroundResource(R.drawable.table_frame_gray);
    				count.setPadding(0, 10, 0, 10);
    				count.setText(aDetailModel.getCount());
    				count.setTextSize(16f);
    				count.setGravity(Gravity.CENTER);
    				
    				TextView amount = new TextView(this);
    				amount.setBackgroundResource(R.drawable.table_frame_gray);
    				amount.setPadding(0, 10, 0, 10);
    				amount.setText(aDetailModel.getEstimated_amount());
    				amount.setTextSize(16f);
    				amount.setGravity(Gravity.CENTER);
    				
    				TextView remark = new TextView(this);
    				remark.setBackgroundResource(R.drawable.table_frame_gray);
    				remark.setPadding(0, 10, 0, 10);
    				remark.setTextSize(16f);
    				remark.setText(aDetailModel.getRemark());
    				remark.setGravity(Gravity.CENTER);
    				
    				row.addView(goods);
    				row.addView(count);
    				row.addView(amount);
    				row.addView(remark);
    				
    			table1.addView(row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.FILL_PARENT));
        
        } 
        if(Activity.RESULT_OK==resultCode){
        	if(data==null){
        		System.out.println("data是否为空========"+data);
        		return;
        	}
            Bundle bundle = data.getExtras();  
        	// 获取相机返回的数据，并转换为Bitmap图片格式  
            Bitmap bitmap = (Bitmap) bundle.get("data");
            image_path=image_path+Calendar.getInstance(Locale.CHINA) + ".jpg";
            
            FileUtil.saveImage(image_path, bitmap);
            
    		TableRow row = new TableRow(this);
			ImageView descrip_image=new ImageView(this);
			descrip_image.setBackgroundResource(R.drawable.table_frame_gray);
			descrip_image.setImageBitmap(BitmapUtil.calculateInSampleSize(bitmap, 1.0, 0.35f, 0.35f));
			TableRow.LayoutParams layoutParams1 = new TableRow.LayoutParams(); 
			layoutParams1.width=0;
			layoutParams1.weight=1f;
			layoutParams1.height=layoutParams1.FILL_PARENT;
			descrip_image.setLayoutParams(layoutParams1);
			descrip_image.setScaleType(ScaleType.FIT_XY);
			descrip_image.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					Intent intent=new Intent(CreateApprovalActivity.this, ShowLargeImageActivity.class);
					intent.putExtra("uri", image_path);
					startActivity(intent);
				 }
				});
			
			
			EditText descrip_text = new EditText(this);
			descrip_text.setBackgroundResource(R.drawable.table_frame_gray);
			descrip_text.setPadding(10, 10, 10, 10);
			TableRow.LayoutParams layoutParams2 = new TableRow.LayoutParams();
			layoutParams2.width=0;
			layoutParams2.weight=5f;
			layoutParams2.height=layoutParams1.FILL_PARENT;
			descrip_text.setLayoutParams(layoutParams2);
			
			ImageView delete_row = new ImageView(this);
			delete_row.setBackgroundResource(R.drawable.table_frame_gray);
			delete_row.setImageResource(R.drawable.oa_selecter_app_del);
			delete_row.setScaleType(ScaleType.FIT_XY);
			TableRow.LayoutParams layoutParams3 = new TableRow.LayoutParams(); 
			layoutParams3.width=0;
			layoutParams3.weight=1f;
			layoutParams3.height=layoutParams1.FILL_PARENT;
			delete_row.setLayoutParams(layoutParams3);
			
			
			row.addView(descrip_image);
			row.addView(descrip_text);
			row.addView(delete_row);
			
		table2.addView(row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.FILL_PARENT));

        }
        super.onActivityResult(requestCode, resultCode, data);  
    }
	
	OnClickListener rowClickEvent=new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			
			TableRow table_row=(TableRow)findViewById(arg0.getId());
			TextView child0 = (TextView)table_row.getChildAt(0);
			System.out.println(child0.getText().toString());
		}
	};
	OnLongClickListener rowLongClickEvent=new OnLongClickListener() {
		

		@Override
		public boolean onLongClick(View arg0) {
			System.out.println("i am long click event");
			return true;
		}
	};
	

	
}
